﻿@model Guid
<script>
    $(document).ready(function () {
        var t = $('#UserList').dataTable({
            "serverSide": true,
            "ajax": {
                "url": "@Url.Action("GetPaged", "User")",
                "type": "POST"
            },
            "columnDefs": [{ sDefaultContent: '', aTargets: ['_all'] }],
            columns: [
                { title: '<label class="checkbox m-l-12 m-t-none m-b-none i-checks"><input type="checkbox" /><i></i></label>', data: function (row, type, set) { return '<label class="checkbox m-l-12 m-t-none m-b-none i-checks"><input type="checkbox" name="cellCheckbox" value="' + row.Id + '" /><i></i></label>'; }, "bStorable": false, width: 30 },
                { title: '账号', data: 'UserName', width: 70, "sClass": "center" },
                { title: "姓名", data: 'PersonInfo.FullName', "sClass": "left" },
                { title: "性别", data: 'PersonInfo.Gender.Name', width: 70, "sClass": "center" }
            ]
        });
    });


    //保存关联
    function SaveItem(url) {
        var userGroupId = "@Model";
        
        var ids = new Array();

        $("#UserList tbody tr").each(function (x) {

            var checkbox = $(this).find("td:eq(0) input");

            if (checkbox.length > 0 && checkbox.prop('checked')) {
                ids.push(checkbox.val());
            }
        });

        if (ids.length === 0) {
            window.app.message("请选中一项。");
            return;
        }


        $.ajax({
            type: "POST",
            url: url,
            data: { "userGroupId": userGroupId, "userIds": ids },
            dataType: "json",
            success: function (data) {
                $("#ajaxModal .modal").modal("toggle");
                if (data.errors.length > 0) {
                    swal("出错啦！", data.errors, "error");
                } else {
                    swal("已经添加", "你已经添加成功！", "success");
                    $('#selectedUsers').DataTable().ajax.reload();
                }
            }
        });
    }

</script>
<div class="modal fade" id="modal">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">选取学生</h4>
            </div>
            <div class="panel modal-body">
                <div class="pull-right text-black pos-rlt m-b-sm" id="SearchInput">
                    <form>
                        <input type="text" class="form-control btn-rounded" id="typehead" name="FullName" placeholder="输入姓名查询" style="height: 30px">
                        <i class="fa fa-search pos-abt" onclick="window.app.search();" style="top: 7px; right: 8px; cursor: pointer;"></i>
                    </form>
                </div>
                <div class="text-left">
                    @Html.Bootstrap().Button("确定").Style(BootstrapStyle.Primary).Click("SaveItem('/UserGroup/Relation');")
                    @Html.Bootstrap().Button("关闭").Style(BootstrapStyle.Default).Attribute("data-dismiss", "modal")
                </div>

                <table id="UserList" class="table table-bordered table-striped"></table>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>